package co2.bean;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.hibernate.id.UUIDHexGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import co2.engine.DAOInterface;
import co2.engine.SessionValidation;
import co2.pojos.BankSoal;
import co2.pojos.HasilTest;
import co2.pojos.MstUser;
import co2.pojos.PaketSoal;

@ManagedBean
@ViewScoped
public class LatihanBean extends AbstractBean implements Serializable {
	private static final long serialVersionUID = 1L;

	private List<BankSoal> listbs;
	private MstUser user;
	private HasilTest hasilTest;
	private Map sessionMap;
	
	@Autowired
	@Qualifier("daoI")
	private DAOInterface dao;
	
	private boolean unassign;
	
	private void shuffleList(List list){
		long seed = System.nanoTime();
		Collections.shuffle(list, new Random(seed));
	}
	
	@PostConstruct
	public void postCon(){
		sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		sessionMap.remove("pesan1");
		
		unassign = false;
		SignInBean signInBean = SessionValidation.checkSession();
		
		if(signInBean!=null){
			user = signInBean.getTheUser();
			if(user.getAssignedPaket()!=null){
				listbs = dao.getBankSoalByPsId(user.getAssignedPaket());
				
				//prepare HasilTest
				hasilTest = new HasilTest();
				hasilTest.setId(new UUIDHexGenerator().generate(null, null).toString().toUpperCase());
				MstUser mu = new MstUser();
				mu.setUserId(user.getUserId());
				hasilTest.setMstUser(mu);
				PaketSoal ps = new PaketSoal();
				ps.setId(user.getAssignedPaket());
				hasilTest.setPaketSoal(ps);
			}else{
				unassign=true;
			} 
		}else{
			try{
				String path = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
				FacesContext.getCurrentInstance().getExternalContext().redirect(path+"/jsf/target/tumbal.jsf");
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	
	public void submit(){
		sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		int benar = 0;
		for(BankSoal bs:listbs){
			if(bs.getKunci().equals(bs.getJwbUser())){
				benar++;
			}
			
//			bs.setJwbUser(null);
		}
		
		if(hasilTest!=null && hasilTest.getId()!=null){
			hasilTest.setBenar(benar);
			hasilTest.setJmlSoal(listbs.size());
			
			try{
				List<HasilTest> listht = dao.hasiltestExist(user.getUserId(), user.getAssignedPaket());
				
				user.setAssignedPaket(null);
				dao.update(user);
				
				if(listht!=null && listht.size()>0){
					hasilTest = (HasilTest) listht.get(0);
					hasilTest.setBenar(benar);
					hasilTest.setJmlSoal(listbs.size());
					dao.update(hasilTest);
				}else{
					dao.save(hasilTest);
				}		
				
				BigDecimal nilai = new BigDecimal(hasilTest.getBenar()).divide(new BigDecimal(hasilTest.getJmlSoal()),1,BigDecimal.ROUND_HALF_EVEN)
						.multiply(new BigDecimal(100));
				
				
				
				StringBuffer sb = new StringBuffer();
				sb.append(nilai.toString()).append(";").append(hasilTest.getBenar());
				sessionMap.put("pesan1", sb.toString());
				
				String requestPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() ;
				FacesContext.getCurrentInstance().getExternalContext().redirect(requestPath+"/jsf/target/latihan_next.jsf");
				
			}catch(Exception e){
				e.printStackTrace();
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error: "+e.getMessage(),""));
			}
		}
		
	}

	
	
	
	
	
	
	
	
	
	//getter and setter
	public List<BankSoal> getListbs() {
		return listbs;
	}

	public void setListbs(List<BankSoal> listbs) {
		this.listbs = listbs;
	}

	public MstUser getUser() {
		return user;
	}

	public void setUser(MstUser user) {
		this.user = user;
	}

	public boolean isUnassign() {
		return unassign;
	}

	public void setUnassign(boolean unassign) {
		this.unassign = unassign;
	}

	public HasilTest getHasilTest() {
		return hasilTest;
	}

	public void setHasilTest(HasilTest hasilTest) {
		this.hasilTest = hasilTest;
	}

	public Map getSessionMap() {
		return sessionMap;
	}

	public void setSessionMap(Map sessionMap) {
		this.sessionMap = sessionMap;
	}

	public DAOInterface getDao() {
		return dao;
	}

	public void setDao(DAOInterface dao) {
		this.dao = dao;
	}
}
